<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="700"
    :before-close="() => cancel()"
  >
    <p class="infoP">基本信息</p>
    <el-form ref="ruleFormRef" :inline="true" :model="ruleForm" :rules="rules">
      <el-form-item label="用户名称" prop="nickName">
        <el-input
          clearable
          v-model="ruleForm.nickName"
          placeholder="请输入用户名称"
        />
      </el-form-item>
      <el-form-item label="归属部门" prop="deptId">
        <p style="color: red">不存在暂时不显示</p>
        <!-- <el-input clearable v-model="ruleForm.deptId" placeholder="请输入权限字符" /> -->
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input clearable v-model="ruleForm.phone" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input clearable v-model="ruleForm.email" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="登录账号" prop="userName">
        <el-input clearable v-model="ruleForm.userName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input
          clearable
          v-model="ruleForm.password"
          placeholder="请输入"
          type="password"
          show-password
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="sex">
        <el-select
          v-model="ruleForm.sex"
          placeholder="请选择"
          clearable
          style="width: 180px"
        >
          <el-option
            v-for="item in sysUserSex.consts"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="deleted">
        <el-switch
          v-model="ruleForm.deleted"
          active-value="0"
          inactive-value="1"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="positionId">
        <p style="color: red">不存在暂时不显示</p>
        <!-- <el-switch v-model="ruleForm.positionId" /> -->
      </el-form-item>
      <el-form-item label="角色" prop="roleId">
        <!-- <el-switch v-model="ruleForm.roleId" /> -->
        <p style="color: red">不存在暂时不显示</p>
      </el-form-item>
    </el-form>
    <p class="infoP">其他信息</p>
    <el-input
      clearable
      v-model="ruleForm.remark"
      placeholder="请输入备注"
      type="textarea"
      :autosize="{ minRows: 4, maxRows: 10 }"
    />
    <template #footer>
      <div class="dialog-footer" style="margin-top: 20px">
        <el-button @click="() => cancel()">取 消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          保 存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { adminUserAdd, adminUserUpdate } from '@/api/system';

import { useDictStore } from '@/store/dict';
const dictStore = useDictStore();
const { sysUserSex } = dictStore;

const { item, type } = defineProps(['type', 'item']);
const emit = defineEmits(['cancel']);

onMounted(() => {
  if (type === 'edit') {
    console.log(item);
    ruleForm.userId = item.userId;
    title.value = '编辑成员';
    Object.assign(ruleForm, item);
  } else {
    title.value = '新增成员';
  }
});

const dialogVisible = ref(true);
const title = ref('新增字典选项');
const ruleFormRef = ref();

const ruleForm = reactive({
  nickName: '',
  deptId: 1,
  phone: '',
  email: '',
  userName: '',
  password: '',
  sex: '',
  deleted: '0',
  positionId: 1,
  roleId: 11,
  remark: '',
});
const rules = reactive({
  nickName: { required: true, message: '请输入用户名称', trigger: 'blur' },
  userName: { required: true, message: '请输入登录账号', trigger: 'blur' },
  password: { required: true, message: '请输入密码', trigger: 'blur' },
});
const submitForm = async (ruleFormRef) => {
  await ruleFormRef.validate((valid) => {
    if (valid) {
      if (type == 'add') {
        console.log(ruleForm);
        adminUserAdd(ruleForm).then(() => {
          cancel(true);
        });
      } else {
        adminUserUpdate(ruleForm).then(() => {
          cancel(true);
        });
      }
    }
  });
};
const cancel = (flag = false) => {
  emit('cancel', flag);
};
</script>

<style lang="less" scoped>
.search-card,
.table-card {
  margin-bottom: 20px;

  .el-card__header {
    padding: 15px 20px;
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .header-buttons {
    display: flex;
    gap: 10px;
  }
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.infoP {
  font-size: 16px;
  font-weight: 600;
  margin: 20px 0;
  padding-bottom: 10px;
  border-bottom: 1px solid;
}
</style>
